﻿using System;
using System.Windows.Forms;
using DevComponents.AdvTree;
using DevComponents.DotNetBar;
using SharpErp.Data.Common.Entity;
using SharpErp.UI.Common;
using SharpErp.UI.Common.Attribute;
using DevComponents.DotNetBar.Controls;
using System.Drawing;
using System.Collections.Generic;
using System.IO;

namespace SharpErp.UI.BasicData
{

    [Menu(Module = MenuModule.BasicInfo, MenuName = "材料信息", IconName = "3d_material_drop_tool_32px_1185750_easyicon.net")]
    public partial class UIMaterialInfoForm : Common.BaseTabForm
    {

        public string GroupId { get; set; }

        public HashSet<int>[] SelectedIds = new HashSet<int>[10];

        private LocalSettingItem LocalSettings { get; set; }

        public UIMaterialInfoForm()
        {
            BeforeLoad += UIMaterialInfoForm_BeforeLoad;
            AfterLoad += UIMaterialInfoForm_AfterLoad;
            InitializeComponent();
        }

        private void UIMaterialInfoForm_AfterLoad(object sender, EventArgs e)
        {
            InitData();
            using (var context = new Data.Common.Context.LocalSettingContext())
            {
                var Materials = context.GetSysLocalSettingItem(Data.Common.Context.LocalSettingContext.LOCAL_SETTING_TAG_MATERIAL);
                if (Materials != null && Materials.Count > 0)
                {
                    LocalSettings = Materials[0];
                }
            }
            if (LocalSettings != null)
            {
                var driver = LocalSettings.Value.Split(new string[] { "=>" }, StringSplitOptions.RemoveEmptyEntries);
                if (new DriveInfo(driver[1]).IsReady)
                {
                }
                else
                {
                    SharpErp.Common.Util.DriveReflection.WNetReflectDrive(driver[1], driver[0], driver.Length > 2 ? driver[2] : null, driver.Length > 3 ? driver[3] : null);
                }
            }
        }

        private void UIMaterialInfoForm_BeforeLoad(object sender, EventArgs e)
        {
            dgClassEight.AutoGenerateColumns = dgClassNine.AutoGenerateColumns = false;
        }

        private void InitData()
        {
            GroupId = "group1";
            dgClassTwo.AutoGenerateColumns = dgClassThree.AutoGenerateColumns =
                dgClassFour.AutoGenerateColumns = dgClassFive.AutoGenerateColumns = dgClassSix.AutoGenerateColumns = dgClassSeven.AutoGenerateColumns = false;
            var MaterialClass = this.Service.GetSysMaterialFirstClass();
            node2.Nodes.Clear();
            foreach (var Material in MaterialClass)
            {
                var node = new Node(Material.Name)
                {
                    CheckBoxStyle = eCheckBoxStyle.RadioButton,
                    CheckBoxVisible = true,
                    DataKey = Material
                };
                node2.Nodes.Add(node);
            }
            this.dgClassTwo.DataSource = this.dgClassThree.DataSource =
                this.dgClassFour.DataSource = this.dgClassFive.DataSource = this.dgClassSix.DataSource =
                this.dgClassEight.DataSource = this.dgClassNine.DataSource = null;
            this.removeDataGridBindings();
            this.addDataGridBindings();
        }

        private void removeDataGridBindings()
        {
            dgClassTwo.CellClick -= DgSecondClassSelectionChanged;
            dgClassThree.CellClick -= DgSecondClassSelectionChanged;
            dgClassFour.CellClick -= DgSecondClassSelectionChanged;
            dgClassFive.CellClick -= DgSecondClassSelectionChanged;
            dgClassSix.CellClick -= DgSecondClassSelectionChanged;
            dgClassSeven.CellClick -= DgSecondClassSelectionChanged;
            dgClassEight.CellClick -= DgSecondClassSelectionChanged;
            dgClassNine.CellClick -= DgSecondClassSelectionChanged;

            dgClassTwo.DataSourceChanged -= DgClassTwo_DataSourceChanged;
            dgClassThree.DataSourceChanged -= DgClassTwo_DataSourceChanged;
            dgClassFour.DataSourceChanged -= DgClassTwo_DataSourceChanged;
            dgClassFive.DataSourceChanged -= DgClassTwo_DataSourceChanged;
            dgClassSix.DataSourceChanged -= DgClassTwo_DataSourceChanged;
            dgClassSeven.DataSourceChanged -= DgClassTwo_DataSourceChanged;
            dgClassEight.DataSourceChanged -= DgClassTwo_DataSourceChanged;
            dgClassNine.DataSourceChanged -= DgClassTwo_DataSourceChanged;
        }

        private void addDataGridBindings()
        {
            dgClassTwo.CellClick += DgSecondClassSelectionChanged;
            dgClassThree.CellClick += DgSecondClassSelectionChanged;
            dgClassFour.CellClick += DgSecondClassSelectionChanged;
            dgClassFive.CellClick += DgSecondClassSelectionChanged;
            dgClassSix.CellClick += DgSecondClassSelectionChanged;
            dgClassSeven.CellClick += DgSecondClassSelectionChanged;
            dgClassEight.CellClick += DgSecondClassSelectionChanged;
            dgClassNine.CellClick += DgSecondClassSelectionChanged;

            dgClassTwo.DataSourceChanged += DgClassTwo_DataSourceChanged; ;
            dgClassThree.DataSourceChanged += DgClassTwo_DataSourceChanged;
            dgClassFour.DataSourceChanged += DgClassTwo_DataSourceChanged;
            dgClassFive.DataSourceChanged += DgClassTwo_DataSourceChanged;
            dgClassSix.DataSourceChanged += DgClassTwo_DataSourceChanged;
            dgClassSeven.DataSourceChanged += DgClassTwo_DataSourceChanged;
            dgClassEight.DataSourceChanged += DgClassTwo_DataSourceChanged;
            dgClassNine.DataSourceChanged += DgClassTwo_DataSourceChanged;
        }

        private void DgClassTwo_DataSourceChanged(object sender, EventArgs e)
        {
            var datagrid = sender as DataGridViewX;
            if (datagrid == null) return;
            var gridGroup = datagrid.Tag.ToString();

            if (gridGroup.CompareTo("group2") >= 0 && gridGroup.CompareTo("group8") <= 0)
            {

                int index = int.Parse(gridGroup.Substring(5));
                SetSelectedIds(datagrid, ref SelectedIds[index]);
                DgSecondClassSelectionChanged(sender, e);
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            var type = OperationType.OpView;
            if (sender == buttonAdd) type = OperationType.OpAdd;
            else if (sender == buttonModify) type = OperationType.OpModify;
            else if (sender == btnAddSub) type = OperationType.OpAdd;
            var dialog = new MaterialDetailInfoDialog
            {
                GroupId = sender == btnAddSub && !string.IsNullOrEmpty(GroupId) ? GroupId.Substring(0, "group".Length) + (int.Parse(GroupId.Substring(5)) + 1) : GroupId,
                OpType = type,
                MaterialFirstClassObject = advTreeItem.SelectedNode != null ? advTreeItem.SelectedNode.DataKey as MaterialFirstClass : null,
                MaterialSecondClassObject = dgClassTwo.CurrentRow != null ? dgClassTwo.CurrentRow.DataBoundItem as MaterialSecondClass : null,
                MaterialThirdClassObject = dgClassThree.CurrentRow != null ? dgClassThree.CurrentRow.DataBoundItem as MaterialThirdClass : null,
                MaterialForthClassObject = dgClassFour.CurrentRow != null ? dgClassFour.CurrentRow.DataBoundItem as MaterialForthClass : null,
                MaterialFifthClassObject = dgClassFive.CurrentRow != null ? dgClassFive.CurrentRow.DataBoundItem as MaterialFifthClass : null,
                MaterialSixClassObject = dgClassSix.CurrentRow != null ? dgClassSix.CurrentRow.DataBoundItem as MaterialSixClass : null,
                MaterialSevenClassObject = dgClassSeven.CurrentRow != null ? dgClassSeven.CurrentRow.DataBoundItem as MaterialSevenClass : null,
                MaterialEightClassObject = dgClassEight.CurrentRow?.DataBoundItem as MaterialEightClass,
                MaterialNineClassObject = dgClassNine.CurrentRow?.DataBoundItem as MaterialNineClass
            };
            if(dialog.OpType == OperationType.OpAdd)
            {
                dialog.OnUpdate = delegate (string groupId, Data.Common.Entity.BaseEntity entity) {
                     switch(groupId)
                    {
                        case "group1":
                            InitData();
                            break;
                        case "group2":
                            var list2 = dgClassTwo.DataSource as List<MaterialSecondClass>;
                            if(list2 != null)
                            {
                                dgClassTwo.DataSource = null;
                            }
                            else
                            {
                                list2 = new List<MaterialSecondClass>();
                            }
                            list2.Add(entity as MaterialSecondClass);
                            dgClassTwo.DataSource = list2;
                            break;
                        case "group3":
                            var list3 = dgClassThree.DataSource as List<MaterialThirdClass>;
                            if(list3 != null)
                            {
                                dgClassThree.DataSource = null;
                            }
                            else
                            {
                                list3 = new List<MaterialThirdClass>();
                            }
                            list3.Add(entity as MaterialThirdClass);
                            dgClassThree.DataSource = list3;
                            break;
                        case "group4":
                            var list4 = dgClassFour.DataSource as List<MaterialForthClass>;
                            if(list4 != null)
                            {
                                dgClassFour.DataSource = null;
                            }
                            else
                            {
                                list4 = new List<MaterialForthClass>();
                            }
                            list4.Add(entity as MaterialForthClass);
                            dgClassFour.DataSource = list4;
                            break;
                        case "group5":
                            var list5 = dgClassFive.DataSource as List<MaterialFifthClass>;
                            if(list5 != null)
                            {
                                dgClassFive.DataSource = null;
                            }
                            else
                            {
                                list5 = new List<MaterialFifthClass>();
                            }
                            list5.Add(entity as MaterialFifthClass);
                            dgClassFive.DataSource = list5;
                            break;
                        case "group6":
                            var list6 = dgClassSix.DataSource as List<MaterialSixClass>;
                            if(list6 != null)
                            {
                                dgClassSix.DataSource = null;
                            }
                            else
                            {
                                list6 = new List<MaterialSixClass>();
                            }
                            list6.Add(entity as MaterialSixClass);
                            dgClassSix.DataSource = list6;
                            break;
                        case "group7":
                            var list7 = dgClassSeven.DataSource as List<MaterialSevenClass>;
                            if(list7 != null)
                            {
                                dgClassSeven.DataSource = null;
                            }
                            else
                            {
                                list7 = new List<MaterialSevenClass>();
                            }
                            list7.Add(entity as MaterialSevenClass);
                            dgClassSeven.DataSource = list7;
                            break;
                        case "group8":
                            var list8 = dgClassEight.DataSource as List<MaterialEightClass>;
                            if(list8 != null)
                            {
                                dgClassEight.DataSource = null;
                            }
                            else
                            {
                                list8 = new List<MaterialEightClass>();
                            }
                            list8.Add(entity as MaterialEightClass);
                            dgClassEight.DataSource = list8;
                            break;
                        case "group9":
                            var list9 = dgClassNine.DataSource as List<MaterialNineClass>;
                            if(list9 != null)
                            {
                                dgClassNine.DataSource = null;
                            }
                            else
                            {
                                list9 = new List<MaterialNineClass>();
                            }
                            list9.Add(entity as MaterialNineClass);
                            dgClassNine.DataSource = list9;
                            break;
                    }
                };

            }
            else if(dialog.OpType == OperationType.OpModify)
            {
                dialog.OnUpdate = delegate (string groupId, Data.Common.Entity.BaseEntity entity)
                {
                    switch (groupId)
                    {
                        case "group1":
                            InitData();
                            break;
                        case "group2":
                            var list2 = dgClassTwo.DataSource as List<MaterialSecondClass>;
                            if (list2 != null)
                            {
                                dgClassTwo.DataSource = null;
                            }
                            else
                            {
                                list2 = new List<MaterialSecondClass>();
                            }
                            dgClassTwo.DataSource = list2;
                            var index = list2.IndexOf((MaterialSecondClass)entity);
                            index = (index + 1) % list2.Count;
                            dialog.MaterialSecondClassObject = list2[index];
                            dialog.MaterialDetailInfoDialogLoad(dialog, null);
                            break;
                        case "group3":
                            var list3 = dgClassThree.DataSource as List<MaterialThirdClass>;
                            if (list3 != null)
                            {
                                dgClassThree.DataSource = null;
                            }
                            else
                            {
                                list3 = new List<MaterialThirdClass>();
                            }
                            dgClassThree.DataSource = list3;
                            index = list3.IndexOf((MaterialThirdClass)entity);
                            index = (index + 1) % list3.Count;
                            dialog.MaterialThirdClassObject = list3[index];
                            dialog.MaterialDetailInfoDialogLoad(dialog, null);
                            break;
                        case "group4":
                            var list4 = dgClassFour.DataSource as List<MaterialForthClass>;
                            if (list4 != null)
                            {
                                dgClassFour.DataSource = null;
                            }
                            else
                            {
                                list4 = new List<MaterialForthClass>();
                            }
                            dgClassFour.DataSource = list4;
                            index = list4.IndexOf((MaterialForthClass)entity);
                            index = (index + 1) % list4.Count;
                            dialog.MaterialForthClassObject = list4[index];
                            dialog.MaterialDetailInfoDialogLoad(dialog, null);
                            break;
                        case "group5":
                            var list5 = dgClassFive.DataSource as List<MaterialFifthClass>;
                            if (list5 != null)
                            {
                                dgClassFive.DataSource = null;
                            }
                            else
                            {
                                list5 = new List<MaterialFifthClass>();
                            }
                            dgClassFive.DataSource = list5;
                            index = list5.IndexOf((MaterialFifthClass)entity);
                            index = (index + 1) % list5.Count;
                            dialog.MaterialFifthClassObject = list5[index];
                            dialog.MaterialDetailInfoDialogLoad(dialog, null);
                            break;
                        case "group6":
                            var list6 = dgClassSix.DataSource as List<MaterialSixClass>;
                            if (list6 != null)
                            {
                                dgClassSix.DataSource = null;
                            }
                            else
                            {
                                list6 = new List<MaterialSixClass>();
                            }
                            dgClassSix.DataSource = list6;
                            index = list6.IndexOf((MaterialSixClass)entity);
                            index = (index + 1) % list6.Count;
                            dialog.MaterialSixClassObject = list6[index];
                            dialog.MaterialDetailInfoDialogLoad(dialog, null);
                            break;
                        case "group7":
                            var list7 = dgClassSeven.DataSource as List<MaterialSevenClass>;
                            if (list7 != null)
                            {
                                dgClassSeven.DataSource = null;
                            }
                            else
                            {
                                list7 = new List<MaterialSevenClass>();
                            }
                            dgClassSeven.DataSource = list7;
                            index = list7.IndexOf((MaterialSevenClass)entity);
                            index = (index + 1) % list7.Count;
                            dialog.MaterialSevenClassObject = list7[index];
                            dialog.MaterialDetailInfoDialogLoad(dialog, null);
                            break;
                        case "group8":
                            var list8 = dgClassEight.DataSource as List<MaterialEightClass>;
                            if (list8 != null)
                            {
                                dgClassEight.DataSource = null;
                            }
                            else
                            {
                                list8 = new List<MaterialEightClass>();
                            }
                            dgClassEight.DataSource = list8;
                            index = list8.IndexOf((MaterialEightClass)entity);
                            index = (index + 1) % list8.Count;
                            dialog.MaterialEightClassObject = list8[index];
                            dialog.MaterialDetailInfoDialogLoad(dialog, null);
                            break;
                        case "group9":
                            var list9 = dgClassNine.DataSource as List<MaterialNineClass>;
                            if (list9 != null)
                            {
                                dgClassNine.DataSource = null;
                            }
                            else
                            {
                                list9 = new List<MaterialNineClass>();
                            }
                            dgClassNine.DataSource = list9;
                            index = list9.IndexOf((MaterialNineClass)entity);
                            index = (index + 1) % list9.Count;
                            dialog.MaterialNineClassObject = list9[index];
                            dialog.MaterialDetailInfoDialogLoad(dialog, null);
                            break;
                    }
                };
            }
            dialog.ShowDialog();
            if (dialog.NeedRefresh)
            {
                switch(dialog.GroupId)
                {
                    case null:
                    case "":
                    case "group1":
                        InitData();
                        break;
                    case "group2":
                        AdvTreeItemSelectionChanged(advTreeItem, null);
                        break;
                    case "group3":
                        DgSecondClassSelectionChanged(dgClassTwo, null);
                        break;
                    case "group4":
                        DgSecondClassSelectionChanged(dgClassThree, null);
                        break;
                    case "group5":
                        DgSecondClassSelectionChanged(dgClassFour, null);
                        break;
                    case "group6":
                        DgSecondClassSelectionChanged(dgClassFive, null);
                        break;
                    case "group7":
                        DgSecondClassSelectionChanged(dgClassSix, null);
                        break;
                    case "group8":
                        DgSecondClassSelectionChanged(dgClassSeven, null);
                        break;
                    case "group9":
                        DgSecondClassSelectionChanged(dgClassEight, null);
                        break;
                }

                if(type == OperationType.OpAdd || type == OperationType.OpModify)
                {
                    DataGridViewX[] dataGridList = { dgClassTwo, dgClassThree, dgClassFour, dgClassFive, dgClassSix, dgClassSeven, dgClassEight, dgClassNine };
                    foreach(var dataGrid  in dataGridList)
                    {
                        if(dataGrid.CurrentRow != null)
                        {
                            GroupId = dataGrid.Tag.ToString();
                        }
                        else
                        {
                            break;
                        }
                    }

                }
            }
        }

        private HashSet<int> GetSelectedIds(DataGridViewX datagrid)
        {
            HashSet<int> ids = new HashSet<int>();
            foreach (var t in datagrid.Rows)
            {
                DataGridViewRow row = t as DataGridViewRow;
                var cell = row.Cells[0] as DataGridViewCheckBoxCell;
                if (cell != null && cell.Value == cell.TrueValue)
                {
                    ids.Add((row.DataBoundItem.GetType().GetProperty("Id").GetValue(row.DataBoundItem) as int?).Value);
                }
            }
            return ids;
        }

        private void SetSelectedIds(DataGridViewX datagrid, ref HashSet<int> ids)
        {
            var newIds = new HashSet<int>();
            foreach (var t in datagrid.Rows)
            {
                DataGridViewRow row = t as DataGridViewRow;
                var cell = row.Cells[0] as DataGridViewCheckBoxCell;
                if (cell != null)
                {
                    int id = (row.DataBoundItem.GetType().GetProperty("Id").GetValue(row.DataBoundItem) as int?).Value;
                    if(ids != null) cell.Value = ids.Contains(id) ? cell.TrueValue : cell.FalseValue;
                    if (cell.Value == cell.TrueValue) newIds.Add(id);
                }
            }
            if(newIds.Count <= 0 && datagrid.RowCount > 0)
            {
                DataGridViewRow row = datagrid.Rows[0] as DataGridViewRow;
                var cell = row.Cells[0] as DataGridViewCheckBoxCell;
                if (cell != null)
                {
                    int id = (row.DataBoundItem.GetType().GetProperty("Id").GetValue(row.DataBoundItem) as int?).Value;
                    cell.Value = cell.TrueValue;
                    if (cell.Value == cell.TrueValue) newIds.Add(id);
                }

            }
            ids = newIds;
        }

        private void DgSecondClassSelectionChanged(object sender, EventArgs e)
        {
            var datagrid = sender as DataGridViewX;
            if (datagrid == null) return;
            var gridGroup = datagrid.Tag.ToString();
            if (e is DataGridViewCellEventArgs)
            {
                GroupId = datagrid.Tag.ToString();
                var cellEvent = e as DataGridViewCellEventArgs;
                if (cellEvent.ColumnIndex >= 0 && cellEvent.RowIndex >= 0)
                {
                    int index = int.Parse(GroupId.Substring(5));
                    SelectedIds[index] = null;
                    for (int row = 0; row < datagrid.RowCount; ++row)
                    {
                        var cell = datagrid.Rows[row].Cells[0] as DataGridViewCheckBoxCell;
                        if (cell != null)
                        {
                            cell.Value = row == cellEvent.RowIndex ? (cell.Value == cell.TrueValue ? cell.FalseValue : cell.TrueValue) : cell.FalseValue;
                            if(row == cellEvent.RowIndex && cell.Value == cell.TrueValue)
                            {
                                var bindObject = datagrid.Rows[row].DataBoundItem;
                                var id = bindObject?.GetType().GetProperty("Id").GetValue(bindObject) as int?;
                                SelectedIds[index] = new HashSet<int> {id.Value};
                            }
                        }
                    }
                }
                else
                {
                    return;
                }
            }
            if (gridGroup == "group2")
            {
                var ids = SelectedIds[2];
                if (ids != null && ids.Count > 0) dgClassThree.DataSource = Service.GetSysMaterialThirdClassByParentIds(ids);
                else dgClassThree.DataSource = null;
            }
            else if (gridGroup == "group3")
            {
                var ids = SelectedIds[3];
                if (ids != null && ids.Count > 0) dgClassFour.DataSource = Service.GetSysMaterialFourClassByParentIds(ids);
                else dgClassFour.DataSource = null;
            }
            else if (gridGroup == "group4")
            {
                var ids = SelectedIds[4];
                if (ids != null && ids.Count > 0) dgClassFive.DataSource = Service.GetSysMaterialFiveClassByParentIds(ids);
                else dgClassFive.DataSource = null;
            }
            else if (gridGroup == "group5")
            {
                var ids = SelectedIds[5];
                if (ids != null && ids.Count > 0) dgClassSix.DataSource = Service.GetSysMaterialSixClassByParentIds(ids);
                else dgClassSix.DataSource = null;
            }
            else if (gridGroup == "group6")
            {
                var ids = SelectedIds[6];
                if (ids != null && ids.Count > 0) dgClassSeven.DataSource = Service.GetSysMaterialSevenClassByParentIds(ids);
                else dgClassSeven.DataSource = null;
            }
            else if (gridGroup == "group7")
            {
                var ids = SelectedIds[7];
                if (ids != null && ids.Count > 0) dgClassEight.DataSource = Service.GetSysMaterialEightClassByParentIds(ids);
                else dgClassEight.DataSource = null;
            }
            else if (gridGroup == "group8")
            {
                var ids = SelectedIds[8];
                if (ids != null && ids.Count > 0) dgClassNine.DataSource = Service.GetSysMaterialNineClassByParentIds(ids);
                else dgClassNine.DataSource = null;
            }
        }

        private void AdvTreeItemSelectionChanged(object sender, EventArgs e)
        {
            GroupId = "group1";
            var node = ((AdvTree)sender).SelectedNode;
            if (node != null)
            {
                node.Checked = true;
                var Material = node.DataKey as MaterialFirstClass;
                if (Material != null)
                {
                    dgClassTwo.DataSource = Service.GetSysMaterialSecondClassByParentId(Material.Id);
                }
                else
                {
                    dgClassTwo.DataSource = null;
                }
            }

        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            TaskDialogInfo confirmDialog = new TaskDialogInfo("警告", eTaskDialogIcon.Delete, "确认删除？", "", eTaskDialogButton.Yes | eTaskDialogButton.No, eTaskDialogButton.No);
            if (TaskDialog.Show(confirmDialog) != eTaskDialogResult.Yes) return;
            var fullNetDirectoryPath = GetCurrentNetDirectory();
            switch (GroupId)
            {
                case "group1":
                    var bindMaterial = advTreeItem.SelectedNode != null ? advTreeItem.SelectedNode.DataKey as MaterialFirstClass : null;
                    if (bindMaterial != null)
                    {
                        Service.DeleteMaterialFirstClass(bindMaterial.Id);
                        ToastNotificationUtil.ShowSuccessToast(this, "删除完成");
                        InitData();
                    }
                    break;
                case "group2":
                    var bindMaterial2 = dgClassTwo.CurrentRow != null ? dgClassTwo.CurrentRow.DataBoundItem as MaterialSecondClass : null;
                    if (bindMaterial2 != null)
                    {
                        Service.DeleteMaterialSecondClass(bindMaterial2.Id);
                        ToastNotificationUtil.ShowSuccessToast(this, "删除完成");
                        InitData();
                    }
                    break;
                case "group3":
                    var bindMaterial3 = dgClassThree.CurrentRow != null ? dgClassThree.CurrentRow.DataBoundItem as MaterialThirdClass : null;
                    if (bindMaterial3 != null)
                    {
                        Service.DeleteMaterialThirdClass(bindMaterial3.Id);
                        ToastNotificationUtil.ShowSuccessToast(this, "删除完成");
                        DgSecondClassSelectionChanged(dgClassTwo, null);
                    }
                    break;
                case "group4":
                    var bindMaterial4 = dgClassFour.CurrentRow != null ? dgClassFour.CurrentRow.DataBoundItem as MaterialForthClass : null;
                    if (bindMaterial4 != null)
                    {
                        Service.DeleteMaterialFurClass(bindMaterial4.Id);
                        ToastNotificationUtil.ShowSuccessToast(this, "删除完成");
                        DgSecondClassSelectionChanged(dgClassThree, null);
                    }
                    break;
                case "group5":
                    var bindMaterial5 = dgClassFive.CurrentRow != null ? dgClassFive.CurrentRow.DataBoundItem as MaterialFifthClass : null;
                    if (bindMaterial5 != null)
                    {
                        Service.DeleteMaterialFiveClass(bindMaterial5.Id);
                        ToastNotificationUtil.ShowSuccessToast(this, "删除完成");
                        DgSecondClassSelectionChanged(dgClassFour, null);
                    }
                    break;
                case "group6":
                    var bindMaterial6 = dgClassSix.CurrentRow != null ? dgClassSix.CurrentRow.DataBoundItem as MaterialSixClass : null;
                    if (bindMaterial6 != null)
                    {
                        Service.DeleteMaterialSixClass(bindMaterial6.Id);
                        ToastNotificationUtil.ShowSuccessToast(this, "删除完成");
                        DgSecondClassSelectionChanged(dgClassFive, null);
                    }
                    break;
                case "group7":
                    var bindMaterial7 = dgClassSeven.CurrentRow != null ? dgClassSeven.CurrentRow.DataBoundItem as MaterialSevenClass : null;
                    if (bindMaterial7 != null)
                    {
                        Service.DeleteMaterialSevenClass(bindMaterial7.Id);
                        ToastNotificationUtil.ShowSuccessToast(this, "删除完成");
                        DgSecondClassSelectionChanged(dgClassSix, null);
                    }
                    break;
                case "group8":
                    var bindMaterial8 = dgClassEight.CurrentRow != null ? dgClassEight.CurrentRow.DataBoundItem as MaterialEightClass : null;
                    if (bindMaterial8 != null)
                    {
                        Service.DeleteMaterialEightClass(bindMaterial8.Id);
                        ToastNotificationUtil.ShowSuccessToast(this, "删除完成");
                        DgSecondClassSelectionChanged(dgClassSeven, null);
                    }
                    break;
                case "group9":
                    var bindMaterial9 = dgClassNine.CurrentRow != null ? dgClassNine.CurrentRow.DataBoundItem as MaterialNineClass : null;
                    if (bindMaterial9 != null)
                    {
                        Service.DeleteMaterialNineClass(bindMaterial9.Id);
                        ToastNotificationUtil.ShowSuccessToast(this, "删除完成");
                        DgSecondClassSelectionChanged(dgClassEight, null);
                    }
                    break;
            }
            if (string.IsNullOrEmpty(fullNetDirectoryPath)) return;
            Directory.Delete(fullNetDirectoryPath, true);
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            this.InitData();
        }

        private void dgClassTwo_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dg = (DataGridView)sender;
            // Current row record
            string rowNumber = (e.RowIndex + 1).ToString();

            // Format row based on number of records displayed by using leading zeros
            while (rowNumber.Length < dg.RowCount.ToString().Length) rowNumber = "0" + rowNumber;

            // Position text
            SizeF size = e.Graphics.MeasureString(rowNumber, this.Font);
            if (dg.RowHeadersWidth < (int)(size.Width + 20)) dg.RowHeadersWidth = (int)(size.Width + 20);

            // Use default system text brush
            Brush b = SystemBrushes.ControlText;

            // Draw row number
            e.Graphics.DrawString(rowNumber, dg.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
        }

        private string GetCurrentNetDirectory()
        {
            if (LocalSettings == null) return null;
            var fullNetDirectoryPath = LocalSettings.Value.Split(new string[] { "=>" }, StringSplitOptions.RemoveEmptyEntries)[1] + DateTime.Now.Year + "年材料信息\\";
            if (GroupId.CompareTo("group1") >= 0)
            {
                var MaterialFirstClassObject = advTreeItem.SelectedNode.DataKey as MaterialFirstClass;
                if (MaterialFirstClassObject == null) return null;
                fullNetDirectoryPath += MaterialFirstClassObject.Name + "\\";
            }
            if (GroupId.CompareTo("group2") >= 0)
            {
                var MaterialSecondClassObject = dgClassTwo.CurrentRow?.DataBoundItem as MaterialSecondClass;
                if (MaterialSecondClassObject == null) return null;
                fullNetDirectoryPath += MaterialSecondClassObject.Name + "\\";
            }
            if (GroupId.CompareTo("group3") >= 0)
            {
                var MaterialThirdClassObject = dgClassThree.CurrentRow?.DataBoundItem as MaterialThirdClass;
                if (MaterialThirdClassObject == null) return null;
                fullNetDirectoryPath += MaterialThirdClassObject.Name + "\\";
            }
            if (GroupId.CompareTo("group4") >= 0)
            {
                var MaterialForthClassObject = dgClassFour.CurrentRow?.DataBoundItem as MaterialForthClass;
                if (MaterialForthClassObject == null) return null;
                fullNetDirectoryPath += "厚" + MaterialForthClassObject.Name + "\\";
            }
            if (GroupId.CompareTo("group5") >= 0)
            {
                var MaterialFifthClassObject = dgClassFive.CurrentRow?.DataBoundItem as MaterialFifthClass;
                if (MaterialFifthClassObject == null) return null;
                fullNetDirectoryPath += MaterialFifthClassObject.Name + "\\";
            }
            if (GroupId.CompareTo("group6") >= 0)
            {
                var MaterialSixClassObject = dgClassSix.CurrentRow?.DataBoundItem as MaterialSixClass;
                if (MaterialSixClassObject == null) return null;
                fullNetDirectoryPath += MaterialSixClassObject.Name + "\\";
            }
            if (GroupId.CompareTo("group7") >= 0)
            {
                var MaterialSevenClassObject = dgClassSeven.CurrentRow?.DataBoundItem as MaterialSevenClass;
                if (MaterialSevenClassObject == null) return null;
                fullNetDirectoryPath += (MaterialSevenClassObject.Standard + "长" + MaterialSevenClassObject.Length + "高" + MaterialSevenClassObject.Width + "\\");
            }
            if (GroupId.CompareTo("group8") >= 0)
            {
                var MaterialEightClassObject = dgClassEight.CurrentRow?.DataBoundItem as MaterialEightClass;
                if (MaterialEightClassObject == null) return null;
                fullNetDirectoryPath += (MaterialEightClassObject.Pcount + MaterialEightClassObject.Unit + "\\");
            }
            return fullNetDirectoryPath;
        }

        private void buttonOpenFolder_Click(object sender, EventArgs e)
        {
            if (LocalSettings == null)
            {
                UI.Common.ToastNotificationUtil.ShowErrorToast(this, "共享网盘尚未设置映射驱动器, 图片不会保存到共享网盘上！");
                return;
            }
            if (LocalSettings != null)
            {
                var fullNetDirectoryPath = GetCurrentNetDirectory();
                if(string.IsNullOrEmpty(fullNetDirectoryPath))
                {
                    SharpErp.Common.Util.Logger.GetLog().Error("directory is empty!");
                    return;
                }
                if(!Directory.Exists(fullNetDirectoryPath))
                {
                    Directory.CreateDirectory(fullNetDirectoryPath);
                }
                if(false && GroupId == "group2")
                {
                    if(Directory.Exists(fullNetDirectoryPath))
                    {
                        var subPath = fullNetDirectoryPath + "各行业黑白快印\\";
                        if (!Directory.Exists(subPath)) Directory.CreateDirectory(subPath);
                        if(Directory.Exists(subPath))
                        {
                            foreach(var subMaterial in new string[]{"建筑业", "农林业", "餐饮业", "房地产业" ,
                            "医药业", "石油化工", "交通运输", "政府机关", "信息地产", "服装业"})
                            {
                                var childPath = subPath + subMaterial + "\\";
                                if (!Directory.Exists(childPath)) Directory.CreateDirectory(childPath);

                            }
                        }
                    }
                }
                System.Diagnostics.Process.Start("explorer.exe", fullNetDirectoryPath);
            }
        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            var selectDialog = new MaterialInfoSelectDialogV2();
            if (selectDialog.ShowDialog() != DialogResult.OK) return;
            var ids = new List<int>();
            if(selectDialog.SelectedMaterialInfo != null)
            {
                foreach (var Material in selectDialog.SelectedMaterialInfo) ids.Add(Material.NineClassId);
            }
            if(ids.Count <= 0)
            {
                UI.Common.ToastNotificationUtil.ShowErrorToast(this, "先选择八类商品再打印！");
                return;
            }

            var dialog = new MaterialInfoPrintDialog();
            var Materials = Service.GetMaterialBarcodeViews(ids);
            Dictionary<int, Data.Common.View.GoodsBarcodeView> map = new Dictionary<int, Data.Common.View.GoodsBarcodeView>();
            foreach (var Material in Materials) map.Add(Material.EightClassId, Material);
            dialog.Materials = new List<Data.Common.View.GoodsBarcodeView>();
            foreach(var id in ids)
            {
                dialog.Materials.Add(map[id]);
            }
            dialog.ShowDialog();
        }

        private void buttonCopyMaterial_Click(object sender, EventArgs e)
        {
            if(GroupId == "group8")
            {
                var ids = SelectedIds[8];
                if(ids == null || ids.Count <= 0)
                {
                    ToastNotificationUtil.ShowErrorToast(this, "请先选择需要复制的八类商品！");
                    return;
                }
                Service.CloneMaterialSevenClass(ids, true);
                ToastNotificationUtil.ShowSuccessToast(this, "复制成功！");
                this.DgSecondClassSelectionChanged(dgClassSeven, null);
            }else if(GroupId == "group9")
            {
                var ids = SelectedIds[9];
                if(ids == null || ids.Count <= 0)
                {
                    ToastNotificationUtil.ShowErrorToast(this, "请先选择需要复制的九类商品！");
                    return;
                }
                Service.CloneMaterialEightClass(ids);
                ToastNotificationUtil.ShowSuccessToast(this, "复制成功！");
                this.DgSecondClassSelectionChanged(dgClassEight, null);

            }
            else
            {
                ToastNotificationUtil.ShowErrorToast(this, "只有八九类商品允许复制！");
            }
        }
    }
}